package com.ijiuchuang.jc_localization.rx;

import com.ijiuchuang.jc_localization.app.Config;
import com.ijiuchuang.jc_localization.bean.cache.DeviceListCache;
import com.ijiuchuang.jc_localization.bean.cache.TagCache;
import com.ijiuchuang.jc_localization.bean.event.BeaconTagData;
import com.ijiuchuang.jc_localization.bean.lite.Tag;
import com.ijiuchuang.jc_localization.tools.utils.Log;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Veev on 2016/6/15
 *
 * @link 384506557@qq.com
 */
public class FilterOnIsShow implements Func1<BeaconTagData, Boolean> {

    private static volatile FilterOnIsShow sInst = null;

    public static FilterOnIsShow getInstance() {
        FilterOnIsShow inst = sInst;
        if (inst == null) {
            synchronized (FilterOnIsShow.class) {
                inst = sInst;
                if (inst == null) {
                    inst = new FilterOnIsShow();
                    sInst = inst;
                }
            }
        }
        return inst;
    }

    private FilterOnIsShow() {
    }

    private LiteOrm liteOrm = Config.liteOrm;
    private TagCache tagCache0;

    @Override
    public Boolean call(final BeaconTagData data) {

        Observable<TagCache> cache = Observable.create(new Observable.OnSubscribe<TagCache>() {
            @Override
            public void call(Subscriber<? super TagCache> subscriber) {
                if (DeviceListCache.getInstance().isCached(data.tagUuid)) {
                    subscriber.onNext(DeviceListCache.getInstance().get(data.tagUuid));
                } else {
                    subscriber.onCompleted();
                }
            }
        });

        Observable<TagCache> database = Observable.create(new Observable.OnSubscribe<TagCache>() {
            @Override
            public void call(Subscriber<? super TagCache> subscriber) {
                QueryBuilder<Tag> equals = new QueryBuilder<>(Tag.class)
                        .whereEquals(Tag.COL_MAC, data.tagUuid);
                if (liteOrm.query(equals) != null) {
                    ArrayList<Tag> list = liteOrm.query(equals);
                    if (list.size() != 0) {
                        TagCache tagCache = new TagCache(list.get(0).getMac(), list.get(0).getName(), list.get(0).isShow());
                        DeviceListCache.getInstance().put(data.tagUuid, tagCache);
                        subscriber.onNext(tagCache);
                    } else {
                        subscriber.onCompleted();
                    }
                } else {
                    subscriber.onCompleted();
                }
            }
        });

        Observable<TagCache> create = Observable.create(new Observable.OnSubscribe<TagCache>() {
            @Override
            public void call(Subscriber<? super TagCache> subscriber) {
                TagCache tagCache = new TagCache(data.tagUuid, "", false);
                Tag tag = new Tag(data.tagUuid, "");
                liteOrm.save(tag);
                Log.i("发现新设备： " + data.tagUuid);
                subscriber.onNext(tagCache);
            }
        });

        Observable.concat(cache, database, create)
                .first()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<TagCache>() {
                    @Override
                    public void call(TagCache tagCache) {
                        tagCache0 = tagCache;
                    }
                });

        return tagCache0.isShow;
    }
}
